package org.jivesoftware.smackx.muc;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

class RoomListenerMultiplexor
  implements ConnectionListener
{
  private static final Map<Connection, WeakReference<RoomListenerMultiplexor>> monitors = new WeakHashMap();
  private Connection connection;
  private RoomMultiplexFilter filter;
  private RoomMultiplexListener listener;

  private RoomListenerMultiplexor(Connection paramConnection, RoomMultiplexFilter paramRoomMultiplexFilter, RoomMultiplexListener paramRoomMultiplexListener)
  {
    if (paramConnection == null)
      throw new IllegalArgumentException("Connection is null");
    if (paramRoomMultiplexFilter == null)
      throw new IllegalArgumentException("Filter is null");
    if (paramRoomMultiplexListener == null)
      throw new IllegalArgumentException("Listener is null");
    this.connection = paramConnection;
    this.filter = paramRoomMultiplexFilter;
    this.listener = paramRoomMultiplexListener;
  }

  private void cancel()
  {
    this.connection.removeConnectionListener(this);
    this.connection.removePacketListener(this.listener);
  }

  public static RoomListenerMultiplexor getRoomMultiplexor(Connection paramConnection)
  {
    synchronized (monitors)
    {
      if (!(monitors.containsKey(paramConnection)))
      {
        RoomListenerMultiplexor localRoomListenerMultiplexor1 = new RoomListenerMultiplexor(paramConnection, new RoomMultiplexFilter(null), new RoomMultiplexListener(null));
        localRoomListenerMultiplexor1.init();
        monitors.put(paramConnection, new WeakReference(localRoomListenerMultiplexor1));
      }
      RoomListenerMultiplexor localRoomListenerMultiplexor2 = (RoomListenerMultiplexor)((WeakReference)monitors.get(paramConnection)).get();
      return localRoomListenerMultiplexor2;
    }
  }

  public void addRoom(String paramString, PacketMultiplexListener paramPacketMultiplexListener)
  {
    this.filter.addRoom(paramString);
    this.listener.addRoom(paramString, paramPacketMultiplexListener);
  }

  public void connectionClosed()
  {
    cancel();
  }

  public void connectionClosedOnError(Exception paramException)
  {
    cancel();
  }

  public void init()
  {
    this.connection.addConnectionListener(this);
    this.connection.addPacketListener(this.listener, this.filter);
  }

  public void reconnectingIn(int paramInt)
  {
  }

  public void reconnectionFailed(Exception paramException)
  {
  }

  public void reconnectionSuccessful()
  {
  }

  public void removeRoom(String paramString)
  {
    this.filter.removeRoom(paramString);
    this.listener.removeRoom(paramString);
  }

  private static class RoomMultiplexFilter
  implements PacketFilter
  {
    private Map<String, String> roomAddressTable = new ConcurrentHashMap();

    public boolean accept(Packet paramPacket)
    {
      boolean bool;
      String str = paramPacket.getFrom();
      if (str == null)
        bool = false;
      while (true)
      {
        return bool;
        bool = this.roomAddressTable.containsKey(StringUtils.parseBareAddress(str).toLowerCase());
      }
    }

    public void addRoom(String paramString)
    {
      if (paramString == null);
      while (true)
      {
        return;
        this.roomAddressTable.put(paramString.toLowerCase(), paramString);
      }
    }

    public void removeRoom(String paramString)
    {
      if (paramString == null);
      while (true)
      {
        return;
        this.roomAddressTable.remove(paramString.toLowerCase());
      }
    }
  }

  private static class RoomMultiplexListener
  implements PacketListener
  {
    private Map<String, PacketMultiplexListener> roomListenersByAddress = new ConcurrentHashMap();

    public void addRoom(String paramString, PacketMultiplexListener paramPacketMultiplexListener)
    {
      if (paramString == null);
      while (true)
      {
        return;
        this.roomListenersByAddress.put(paramString.toLowerCase(), paramPacketMultiplexListener);
      }
    }

    public void processPacket(Packet paramPacket)
    {
      String str = paramPacket.getFrom();
      if (str == null);
      while (true)
      {
        PacketMultiplexListener localPacketMultiplexListener;
        do
        {
          return;
          localPacketMultiplexListener = (PacketMultiplexListener)this.roomListenersByAddress.get(StringUtils.parseBareAddress(str).toLowerCase());
        }
        while (localPacketMultiplexListener == null);
        localPacketMultiplexListener.processPacket(paramPacket);
      }
    }

    public void removeRoom(String paramString)
    {
      if (paramString == null);
      while (true)
      {
        return;
        this.roomListenersByAddress.remove(paramString.toLowerCase());
      }
    }
  }
}